<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_paper extends Controller_Abstract {
	

	//试卷
	function actionindex(){
		$append = array("acct_id"=>$_SESSION['acct']['id']);

		$this->union_table = paper::meta();
		if($this->_context->chapter_id){
			$this->_view['chapter_id']=$this->_context->chapter_id;
		}
		if($this->_context->generalcourse_id){
			$this->_view['generalcourse_id']=$this->_context->generalcourse_id;
		}
		$this->lists($append);
	}
	function actionAdd(){
		//设置要操作的表单，并设置自动插入方式
	    $this->union_table = paper::meta ();
	    $handle = new helper_autoinsert ( $this->union_table );
	    $form = new Form_paper_add ( url ( $this->_context->controller_name . '/' . $this->_context->action_name ) );
 
	    //这里面会自动将get方式获取的参数传递到表单里面去的
	    $result = $handle->add ( $form, 'id', $_GET );
	    if ($this->_context->isGET ()) {
	        //传递$form值
	        $this->_view ['form'] = $result;
	        $this->_viewname = '../form/add';
	        $form->element('acct_id')->value= $_SESSION['acct']['id'];
	    } else {
	        //记录日志
	        if(!$_POST['id'])	{
	            Syslog::logs ( '添加试卷,id :' . $handle->form_database_obj->id );
	        }else{
	            Syslog::logs ( '编辑试卷,id :' . $handle->form_database_obj->id );
	        }
	        $url=url("paper");
	        if($this->_context->chapter_id>0){
	        	$url.="/index/chapter_id/".$this->_context->chapter_id;
	        }
	        if($this->_context->generalcourse_id>0){
	        	$url.="/index/generalcourse_id/".$this->_context->generalcourse_id;
	        }
	        if($handle->status=='success')
	            $this->_context->forwardUrl = $url;
	        return call_user_func_array(array($this,$handle->status),array($result));
	    }
	}
	function actionDel(){
		Syslog::logs("删除试卷，ID：".print_r($this->_context->ids,true));
	    $this->union_table = paper::meta();
	    $this->delete(array('id'=>$this->_context->ids));
	}

	function actionexam(){
		$this->union_table = paperexam::meta();
		//获取搜索参数
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		$this->param = array_merge($this->param,array('type'=>2));

		$dis_param = new helper_disposeparam($this->param);
		$sql = $dis_param->getsql();//OUT PUT用

		$_SESSION['lastsql'] = $sql;//OUT PUT用
		$obj = call_user_func_array(array($this->union_table,'find'), $sql);
		$item = $obj->limitPage($this->curr_page,$this->page_size)
					->order(' '.$this->orderkey.' '.$this->ordertype)
					// ->joinLeft('acct_user','nickname','edu_paper_exam.user_id=acct_user.id')
					// ->joinLeft('edu_paper_paper','title','edu_paper_exam.paper_id=edu_paper_paper.id')
					->fetchPagination($this->pagination)
					->asArray()
					->getall();
		$this->_view['item'] = $item;
		$this->_view['pagination'] = $this->pagination;
		$this->_view['orderField'] = $this->orderkey;
		$this->_view['orderDirection'] = $this->ordertype;

	}

	function actionexamdel(){
		Syslog::logs("删除考试，ID：".print_r($this->_context->ids,true));
	    

	    QDB::getConn()->startTrans();
		try {
			$this->union_table = paperexam::meta();
		    $this->union_table->deleteWhere(array('id'=>$this->_context->ids));
		    $this->union_table = paperquestiondolog::meta();
		    $this->union_table->deleteWhere(array('exam_id'=>$this->_context->ids));
		    QDB::getConn()->completeTrans(true);	
		} catch (Exception $e) {
		    QDB::getConn()->completeTrans(FALSE);
	    	return $this->error($e->getmessage());	
		}
		return $this->success('删除成功');

	}

	function actiondolog(){
		$this->union_table = paperquestiondolog::meta();
		//获取搜索参数
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		
		$dis_param = new helper_disposeparam($this->param);
		$sql = $dis_param->getsql();//OUT PUT用

		$_SESSION['lastsql'] = $sql;//OUT PUT用
		$obj = call_user_func_array(array($this->union_table,'find'), $sql);
		$item = $obj->limitPage($this->curr_page,$this->page_size)
					->order(' '.$this->orderkey.' '.$this->ordertype)
					->joinLeft('edu_paper_questions','title,answer as qanswer','edu_paper_questions_dolog.questions_id=edu_paper_questions.id')
					// ->joinLeft('edu_paper_paper','title','edu_paper_exam.paper_id=edu_paper_paper.id')
					->fetchPagination($this->pagination)
					->asArray()
					->getall();
		$all_count = intval(paperquestiondolog::find('exam_id=?',$this->_context->exam_id)->getCount());
		$right_count = intval(paperquestiondolog::find('exam_id=? and isright=1',$this->_context->exam_id)->getCount());
		$this->_view['all_count'] = $all_count;
		$this->_view['right_count'] = $right_count;
		$this->_view['item'] = $item;
		$this->_view['pagination'] = $this->pagination;
		$this->_view['orderField'] = $this->orderkey;
		$this->_view['orderDirection'] = $this->ordertype;

	}


}


